package com.home766.controller.pad;

import com.home766.entity.Scheme;
import com.home766.service.SchemeService;
import net.biz.Page;
import net.biz.Pageable;
import net.biz.controller.shop.BaseController;
import net.biz.entity.*;
import net.biz.entity.Product.OrderType;
import net.biz.entity.Tag.Type;
import net.biz.service.ProductCategoryService;
import net.biz.service.ProductService;
import net.biz.service.TagService;
import net.biz.util.ParamsUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * pad端-产品controller
 */

@Controller("padProductController")
@RequestMapping("/pad/product")
public class PadProductController extends BaseController {
	
	@Resource(name="productServiceImpl")
	private ProductService productService;
	@Resource(name="tagServiceImpl")
	private TagService tagService;
	@Resource(name="schemeServiceImpl")
	private SchemeService schemeService;
	@Resource(name = "productCategoryServiceImpl")
	private ProductCategoryService productCategoryService;
	/**
	 * 产品展示列表查询
	 * @param model
	 * @param pageable
	 * @return
	 */
	@RequestMapping(value="/list",method = RequestMethod.GET)
	public String list(ModelMap model, Pageable pageable, Integer pageNumber, Long tagId, Integer isPriceAsc){
		List<Tag> ptags = new ArrayList<Tag>();
		if(tagId != null){
			Tag tag = tagService.find(tagId);
			ptags.add(tag);			
		}
		OrderType orderType = null;
		if(isPriceAsc != null){
			if(isPriceAsc == 1){
				orderType = OrderType.priceAsc;
			}else{
				orderType = OrderType.priceDesc;
			}
		}
		if(pageNumber != null){
			pageable.setPageNumber(pageNumber);
		}
		pageable.setPageSize(ParamsUtil.PAD_PAGE_PAGESIZE);
		Page<Product> page = productService.findPage(null, null, null, ptags, null, null, null, true, null, null, false, null, null, orderType, pageable);
		List<Tag> tags = tagService.findList(Type.product);
		model.addAttribute("page", page);
		model.addAttribute("tags", tags);
		model.addAttribute("tagId", tagId);
		return "/pad/product/list";
	}
	
	/**
	 * 产品详情查看
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/content",method = RequestMethod.GET)
	public String content(ModelMap model, Long id){
		if(id == null){
			return PAD_ERROR_VIEW;
		}
		Product product = productService.find(id);
		if(product == null){
			return PAD_ERROR_VIEW;
		}
		String fullName = product.getFullName();
		for(Specification spec : product.getSpecifications()){
			List<SpecificationValue> specificationValues = new ArrayList<SpecificationValue>();
			for(SpecificationValue value : spec.getSpecificationValues()){
				if(fullName.contains(value.getName())){
					specificationValues.add(value);
					break;
				}
			}
			spec.setSpecificationValues(specificationValues);
		}
		model.addAttribute("product", product);
		return "/pad/product/content";
	}
	
	
	/**
	 * pad端商品-特惠活动
	 * @param model
	 * @param pageable
	 * @return
	 */
	@RequestMapping(value="/tehui",method = RequestMethod.GET)
	public String preferentialActivity(ModelMap model, Long productCategoryId, Pageable pageable, Integer isProduct, Integer pageNumber){
		ProductCategory productCategory = productCategoryService.find(productCategoryId);
		pageable.setPageSize(ParamsUtil.PAD_PAGE_PAGESIZE);
		if(isProduct == null){
			isProduct = 1 ;
		}
		if(pageNumber != null && pageNumber > 0){
			pageable.setPageNumber(pageNumber);
		}
		if(isProduct == 1){
			//特惠商品
			List<Tag> tags = new ArrayList<Tag>();
			Tag tag = tagService.find(ParamsUtil.PRODUCT_TEHUI_ACTIVITY);
			Page<Product> page = new Page<Product>();
			if(tag != null){
				tags.add(tag);
				if(productCategoryId==null){
					page = productService.findPage(productCategory, null, null, tags, null, null, null, true, null, null, false, null, null, null, pageable);
				}else if(productCategoryId!=null){
					if(productCategory==null){
						page = null;
					}else{
						page = productService.findPage(productCategory, null, null, tags, null, null, null, true, null, null, false, null, null, null, pageable);
					}
				}
			}			
			model.addAttribute("page", page);
		}else{
			//特惠方案
			Boolean isTehui = true;
			Page<Scheme> page = schemeService.findPage(pageable, null, null,null,null,null,null,true,isTehui);
			model.addAttribute("page", page);
		}
		model.addAttribute("isProduct", isProduct);
		model.addAttribute("pageNumber",pageable.getPageNumber());
		model.addAttribute("productCategoryId",productCategoryId);
		return "/pad/product/preferential";
	}
}
